/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.tools.ClassToolsUtil;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.manager.DeptManager;
import com.apache.uct.util.UctStringUtil;

/**
 * description:  获取部门列表插件
 * @author dwusoft 创建时间：2015-3-26  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class DeptListPlugin implements PluginConnector {

	private Logger log = Logger.getLogger(DeptListPlugin.class);
	private IDao deptDao;

	private DeptManager deptManager;

	/**
	 * TODO 获取部门列表插件.  <br>
	 *  参数说明:<br>
	 *  dept属性:按部门属性查询部门列表;<br>
	 *  分页设置<br>
	 *  pageSize:每页记录数
	 *  pageIndex:当前页码
	 */
	public Object execute(ParamsVo vo) throws Exception {
		String orderBy = "create_time";
		ResultEntity result = new ResultEntity();
		ParamsVo<Dept> deptVo = new ParamsVo<Dept>();

		Dept dept = new Dept();
		dept = (Dept) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dept);
		deptVo.setObj(dept);
		try {
			result.setResult("true");
			MethodParam param = setMethodParams(vo, 2, orderBy);
			if (Validator.isEmpty(param)) {
				result.setMessage("查询条件为空或不存在相关信息");
				return result;
			}
			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			if (pageSize == 0 && pageIndex == 0) {
				List<Dept> list = deptManager.getList(deptVo);
				result.setEntity(list);
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				param.setOrderby(orderBy + " desc");
				Page page = deptDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			result.setResult("false");
			log.error("执行机构查询失败 ", e);
			result.setMessage("机构查询失败！");
		}
		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
		StringBuffer sb = new StringBuffer(deptDao.getSql(type));
		MethodParam param = new MethodParam("ByProperty", "", "", Dept.class.getName());
		String str = buildParams(param, vo);
		if (Validator.isNull(str)) {
			return null;
		}
		sb.append(str);

		if (Validator.isNotNull(orderBy)) {
			sb.append(" order by " + UctStringUtil.toCamelNamed(orderBy) + " desc");
		}
		param.setSqlStr(sb.toString());
		return param;
	}

	private String buildParams(MethodParam param, ParamsVo vo) {
		StringBuffer sb = new StringBuffer();
		if (Validator.isNotNull(String.valueOf(vo.getParams("sysEname")))) {
			sb.append(" and sysEname = :sysEname");
			param.setParams("sysEname", String.valueOf(vo.getParams("sysEname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgId")))) {
			sb.append(" and orgId = :orgId");
			param.setParams("orgId", String.valueOf(vo.getParams("orgId")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("deptId")))) {
			sb.append(" and deptId = :deptId");
			param.setParams("deptId", String.valueOf(vo.getParams("deptId")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgEname")))) {
			sb.append(" and orgEname = :orgEname");
			param.setParams("orgEname", String.valueOf(vo.getParams("orgEname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("deptCode")))) {
			sb.append(" and deptCode = :deptCode");
			param.setParams("deptCode", String.valueOf(vo.getParams("deptCode")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("deptEname")))) {
			sb.append(" and deptEname = :deptEname");
			param.setParams("deptEname", String.valueOf(vo.getParams("deptEname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("fullEname")))) {
			sb.append(" and fullEname = :fullEname");
			param.setParams("fullEname", String.valueOf(vo.getParams("fullEname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("fullCname")))) {
			sb.append(" and fullCname = :fullCname");
			param.setParams("fullCname", String.valueOf(vo.getParams("fullCname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("deptCname")))) {
			sb.append(" and deptCname = :deptCname");
			param.setParams("deptCname", String.valueOf(vo.getParams("deptCname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("fatherId")))) {
			sb.append(" and fatherId = :fatherId");
			param.setParams("fatherId", String.valueOf(vo.getParams("fatherId")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("deptRemark")))) {
			sb.append(" and deptRemark = :deptRemark");
			param.setParams("deptRemark", String.valueOf(vo.getParams("deptRemark")));
		}
		return sb.toString();
	}

	public void setDeptDao(IDao deptDao) {
		this.deptDao = deptDao;
	}

	public void setDeptManager(DeptManager deptManager) {
		this.deptManager = deptManager;
	}

}
